package com.itheima.jdbcTransfer.dao.impl;

import com.itheima.jdbcTransfer.dao.interFace.TransferDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TransferDaoImpl implements TransferDao {
    @Override
    public int transferAdd(Connection con, String toName, Double money) throws SQLException {
        PreparedStatement pst = null;
        int i = 0;
        try {
            String sql = "update tran set money=money+? where name = ?";
            pst = con.prepareStatement(sql);
            pst.setDouble(1,money);
            pst.setString(2,toName);
            i = pst.executeUpdate();
        } finally {
            pst.close();
            return i;
        }

    }

    @Override
    public int transferDel(Connection con, String forName, Double money) throws Exception {
        PreparedStatement pst = null;
        int i = 0;
        try {
            String sql = "update tran set money=money+? where name = ?";
            pst = con.prepareStatement(sql);
            pst.setDouble(1,money);
            pst.setString(2,forName);
            i = pst.executeUpdate();
        } finally {
            pst.close();
            return i;
        }
    }
}
